package com.dongdongshop.controller;

import com.dongdongshop.aop.DKLog;
import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.PageUtil;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.model.TbBrand;
import com.dongdongshop.domain.vo.BrandVO;
import com.dongdongshop.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
@CrossOrigin  //跨域
@Api("运营商后台管理——品牌管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class BrandController {

    @Autowired
    private BrandService brandService;

    //分页查询
    @PostMapping("brandList")
    @ApiOperation(value = "分页模糊查询")
    @ApiImplicitParam(name = "parms", value = "分页工具类", required = true, paramType = "body")
    public Result brandList(@RequestBody PageResult<BrandVO> parms) {
        PageUtil<BrandVO> page = brandService.brandList(parms);
        return Result.OK().setData(page);
    }

    //增加
    @PostMapping("add")
    @ApiOperation(value = "增加品牌")
    @ApiImplicitParam(name = "brand", value = "品牌类", required = false, paramType = "body")
    public Result addBrand(@RequestBody BrandVO brand) {
        brandService.addBrand(brand);
        return Result.OK();
    }

    //回显 根据id查询
    @PostMapping("selectById")
    @ApiOperation(value = "根据id查询数据")
    @ApiImplicitParam(name = "id", value = "主键id", required = false, paramType = "query")
    public Result selectById(@RequestParam Long id) {
        BrandVO brandVO = brandService.selectById(id);
        return Result.OK().setData(brandVO);
    }

    //修改
    @PutMapping("update")
    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "brand", value = "品牌类", required = false, paramType = "body")
    public Result updateBrand(@RequestBody BrandVO brand) {
        brandService.updateBrand(brand);
        return Result.OK();
    }

    //删除
    @DeleteMapping("delete")
    @ApiOperation(value = "根据id单个删除")
    @ApiImplicitParam(name = "id", value = "主键id", required = false, paramType = "query")
    public Result delete(@RequestParam Long id) {
        brandService.delete(id);
        return Result.OK();
    }

    //批量删除
    @DeleteMapping("deleteBrand")
    @ApiOperation(value = "根据id批量删除")
    @ApiImplicitParam(name = "id", value = "主键id", required = false, paramType = "body")
    public Result deleteBrand(@RequestBody List<Long> id) {
        brandService.deleteBrand(id);
        return Result.OK();
    }

    //查询全部
    @GetMapping("getAllBrand")
    @ApiOperation(value = "查询全部")
    public Result getAllBrand() {
        List<BrandVO> getAllBrand = brandService.getAllBrand();
        return Result.OK().setData(getAllBrand);
    }
}
